%{
#include "admin.h"
#include "parser.tab.h"

extern "C" {

int yylex(void);
extern void yyerror(const char *s, ...);
extern int yywrap();

}

int old_status[100];
int status_index = 0;
char gbuf[1024*1024];
int gindex;
extern YYSTYPE yylval;
extern TypeStat typestat;
%}


DIGIT 	[0-9]
LETTER	[A-Za-z]
SPACE	\ |\t
LINE	\n
NUM	({DIGIT}+|{DIGIT}+\.{DIGIT}+)
ID	{LETTER}({LETTER}|{DIGIT})*
STRING \".*\"
INCLUDE_FILE {LETTER}({LETTER}|{DIGIT}|\/)*\.h
ID2	{LETTER}({LETTER}|{DIGIT}|_)*

%x STATUS_COMMENT

%%
 /*字符串*/
{STRING} {
	yylval = yytext;
 	return STRING;
}
 /*16进制数字*/
0x({LETTER}|{DIGIT})+ {
	yylval = yytext;
	return HEXNUMBER;	
}
[<]		{
	yylval = yytext;
	return LAB;
}
[>]		{
	yylval = yytext;
	return RAB;
}
[(]		{
	yylval = yytext;
	return LPT;
}
[)]		{
	yylval = yytext;
	return RPT;
}
[[]		{
	yylval = yytext;
	return LBB;
}
[]]		{
	yylval = yytext;
	return RBB;
}
[#] {
	yylval = yytext;
	return WELL_NUMBER;	
}
[*] {
	yylval = yytext;
	return MUL;	
}
[|][|] {
	yylval = yytext;
	return OR;		
}
[&][&] {
	yylval = yytext;
	return AND;	
}
[=][=] {
	yylval = yytext;
	return EQ;	
}
[!] {
	yylval = yytext;
	return NEQ;	
}
[+] {
	yylval = yytext;
	return ADD;	
}
[-] {
	yylval = yytext;
	return SUB;	
}
[/] {
	yylval = yytext;
	return DIV;	
}
[\%] {
	yylval = yytext;
	return PER;	
}
{NUM} {
	yylval = yytext;
	return NUMBER;		
}
 /******************************注释解析**********************************/
 /*行注释*/
"//".*			{
	yylval = yytext;
	return COMMENTS;
}
 /*多行注释*/
"/*" {
	old_status[status_index++] = YY_START;
	BEGIN STATUS_COMMENT;
	gindex = 0;
	gbuf[gindex++] = '/';
	gbuf[gindex++] = '*';
}
<STATUS_COMMENT>.|\n {
	gbuf[gindex++] = *yytext;
}
<STATUS_COMMENT>"*/" {
	gbuf[gindex++] = '*';
	gbuf[gindex++] = '/';
	gbuf[gindex++] = 0;
	yylval = gbuf;
	BEGIN old_status[--status_index];
	return COMMENTS;
}

 /***********************************全词匹配**********************************************/
"char" {
	yylval = yytext;
 	return CHAR;
}

"int" {
	yylval = yytext;
 	return INT;
}

"short" {
	yylval = yytext;
 	return SHORT;
}

"long" {
	yylval = yytext;
 	return LONG;
}

"signed" {
	yylval = yytext;
 	return SIGNED;
}

"unsigned" {
	yylval = yytext;
 	return UNSIGNED;
}

  /*include*/
"include"	{
	yylval = yytext;
 	return INCLUDE;
}

 /*sizeof*/
"sizeof" {
	yylval = yytext;
 	return SIZEOF;
}

 /*define*/
"define" {
	yylval = yytext;
 	return DEFINE;	
}

 /*if/endif*/
"if" {
	yylval = yytext;
 	return IF;	
}

"elif" {
	yylval = yytext;
 	return ELIF;	
}

"else" {
	yylval = yytext;
 	return ELSE;	
}

"endif" {
	yylval = yytext;
 	return ENDIF;	
}

{INCLUDE_FILE} {
	yylval = yytext;
 	return HEADERFILE;	
}

 /********undef***************/
undef {
	yylval = yytext;
 	return UNDEF;	
}

{ID2} {
	int dst_type = WORD;
	TypeItem *pi = typestat.GetTypeItem(yytext);
	if (pi) {
		dst_type = pi->key;
	}
	yylval = yytext;
 	return dst_type;	
}

 /*其他忽略*/
.|\n
%%
